<template>
  <div class="line-manage-page">
    <span>线路管理</span>

    <el-card class="card">
      <el-form ref="form" :model="queryParams" label-position="right" label-width="100px">
        <el-row type="flex" justify="space-between" align="middle" :gutter="10">
          <el-col>
            <el-form-item label="线路编号：" prop="number">
              <el-input v-model="queryParams.number" placeholder="请输入线路编号" />
            </el-form-item>
          </el-col>

          <el-col>
            <el-form-item label="线路名称：" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入线路名称" />
            </el-form-item>
          </el-col>

          <el-col>
            <el-form-item label="起始机构：">
              <el-cascader
                v-model="selectStartOrganIdArr"
                :options="organizationOptionList"
                :props="treeDefaultProps"
                clearable
              >
                <template slot-scope="{ data }">
                  <span style="color:black;">{{ data.name }}</span>
                </template>
              </el-cascader>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex" justify="start" align="middle" :gutter="10">
          <el-col :span="8">
            <el-form-item label="目的地机构：">
              <el-cascader
                v-model="selectEndOrganIdArr"
                :options="organizationOptionList"
                :props="treeDefaultProps"
                clearable
              >
                <template slot-scope="{ data }">
                  <span style="color:black;">{{ data.name }}</span>
                </template>
              </el-cascader>
            </el-form-item>
          </el-col>

          <el-col style="margin-bottom: 22px;">
            <el-button type="primary" @click="getTransportLineList">搜索</el-button>
            <el-button plain @click="resetSearchParams">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card class="card">
      <div slot="header" class="clearfix">
        <el-row type="flex" justify="start" align="middle">
          <el-button type="primary">新增线路</el-button>
          <el-button type="primary">成本设置</el-button>
          <el-button type="primary">调度配置</el-button>
        </el-row>
      </div>
      <!-- 表格 -->
      <el-table :data="list" style="width: 100%">
        <el-table-column
          prop="number"
          label="线路编号"
        />
        <el-table-column
          prop="name"
          label="线路名称"
        />
        <el-table-column
          prop="date"
          label="线路类型"
        >
          <template #default="{ row }">
            <span v-if="row.type === 1">干线</span>
            <span v-if="row.type === 2">支线</span>
            <span v-if="row.type === 3">接驳路线</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="startOrganName"
          label="起始机构"
        />
        <el-table-column
          prop="endOrganName"
          label="目的地机构"
        />
        <el-table-column
          prop="distance"
          label="距离（千米）"
        />
        <el-table-column
          prop="cost"
          label="成本（元）"
        />
        <el-table-column
          prop="time"
          label="预计时间（分钟）"
          width="150px"
        />
        <el-table-column
          label="操作"
          width="180px"
        >
          <template>
            <el-button class="blue-text" type="text">修改</el-button>
            <el-button type="text">删除</el-button>
            <el-button class="blue-text" type="text">增加车次</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-row style="height: 60px" align="middle" type="flex" justify="center">
        <el-pagination
          :current-page.sync="queryParams.page"
          :page-size="queryParams.pageSize"
          :total="counts"
          layout="total, prev, pager, next"
          @current-change="handlePageChange"
        />
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { getOrganizationTreeList } from '@/api/organization'
import { getTransportLineList } from '@/api/line-manage'

export default {
  name: 'LineManagePage',
  data() {
    return {
      // 机构下拉列表
      organizationOptionList: [],
      // 树形结构的字段映射配置
      treeDefaultProps: {
        // 是否严格的遵守父子节点不互相关联
        checkStrictly: true,
        label: 'name',
        children: 'children',
        value: 'id'
      },
      // 选择的起始机构Id数组
      selectStartOrganIdArr: [],
      // 选择的终点机构Id数组
      selectEndOrganIdArr: [],
      queryParams: {
        // 路线编号
        number: '',
        // 路线名称
        name: '',
        // 起点机构id
        startOrganId: '',
        // 终点机构id
        endOrganId: '',
        // 页码
        page: 1,
        // 一页多少条
        pageSize: 5
      },
      // 线路列表
      list: [],
      // 总条数
      counts: 0
    }
  },
  created() {
    this.getOrganizationTreeList()
    this.getTransportLineList()
  },
  methods: {
    // 获取机构树形列表
    async getOrganizationTreeList() {
      const result = await getOrganizationTreeList()
      console.log(result)
      const dataJson = result.data
      // 解析JSON为树形数据
      this.organizationOptionList = JSON.parse(dataJson) || []
    },
    // 获取线路列表
    async getTransportLineList() {
      // 处理起始机构Id和终点机构Id
      this.queryParams.startOrganId = this.selectStartOrganIdArr ? this.selectStartOrganIdArr[this.selectStartOrganIdArr.length - 1] : null
      this.queryParams.endOrganId = this.selectEndOrganIdArr ? this.selectEndOrganIdArr[this.selectEndOrganIdArr.length - 1] : null
      // 发起请求
      const result = await getTransportLineList(this.queryParams)
      console.log(result)
      // 更新数据
      this.list = result.data.items
      this.counts = +result.data.counts
    },
    resetSearchParams() {
      // 重置表单
      this.$refs.form.resetFields()
      // 重置起始机构
      this.selectStartOrganIdArr = []
      this.queryParams.startOrganId = ''
      // 重置起始机构
      this.selectEndOrganIdArr = []
      this.queryParams.endOrganId = ''
      // 重新发请求
      this.getTransportLineList()
    },
    // 处理分页切换
    handlePageChange(page) {
      this.queryParams.page = page
      this.getTransportLineList()
    }
  }
}
</script>

<style lang="scss" scoped>
.line-manage-page {
  padding: 20px;
  background-color: #f3f4f7;
  padding-top: 23px;
  min-height: 100vh;

  .card {
    margin-top: 20px;
  }

  .blue-text {
    color: #419eff;
  }
}
</style>
